package org.dromara.agriculture.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.agriculture.domain.vo.AgricultureCropBatchVo;
import org.dromara.agriculture.domain.bo.AgricultureCropBatchBo;
import org.dromara.agriculture.service.IAgricultureCropBatchService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 作物批次
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agriculture/cropBatch")
public class AgricultureCropBatchController extends BaseController {

    private final IAgricultureCropBatchService agricultureCropBatchService;

    /**
     * 查询作物批次列表
     */
    @SaCheckPermission("agriculture:cropBatch:list")
    @GetMapping("/list")
    public TableDataInfo<AgricultureCropBatchVo> list(AgricultureCropBatchBo bo, PageQuery pageQuery) {
        return agricultureCropBatchService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出作物批次列表
     */
    @SaCheckPermission("agriculture:cropBatch:export")
    @Log(title = "作物批次", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgricultureCropBatchBo bo, HttpServletResponse response) {
        List<AgricultureCropBatchVo> list = agricultureCropBatchService.queryList(bo);
        ExcelUtil.exportExcel(list, "作物批次", AgricultureCropBatchVo.class, response);
    }

    /**
     * 获取作物批次详细信息
     *
     * @param batchId 主键
     */
    @SaCheckPermission("agriculture:cropBatch:query")
    @GetMapping("/{batchId}")
    public R<AgricultureCropBatchVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long batchId) {
        return R.ok(agricultureCropBatchService.queryById(batchId));
    }

    /**
     * 新增作物批次
     */
    @SaCheckPermission("agriculture:cropBatch:add")
    @Log(title = "作物批次", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgricultureCropBatchBo bo) {
        return toAjax(agricultureCropBatchService.insertByBo(bo));
    }

    /**
     * 修改作物批次
     */
    @SaCheckPermission("agriculture:cropBatch:edit")
    @Log(title = "作物批次", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgricultureCropBatchBo bo) {
        return toAjax(agricultureCropBatchService.updateByBo(bo));
    }

    /**
     * 删除作物批次
     *
     * @param batchIds 主键串
     */
    @SaCheckPermission("agriculture:cropBatch:remove")
    @Log(title = "作物批次", businessType = BusinessType.DELETE)
    @DeleteMapping("/{batchIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] batchIds) {
        return toAjax(agricultureCropBatchService.deleteWithValidByIds(List.of(batchIds), true));
    }
}
